#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>

/* seconddrvtest 
  */
int main(int argc, char **argv)
{
	int fd;
	unsigned char key_vals[256];
	unsigned char ec_line,ec_row,index_num;
	char *endstr;
	int val;
	
	fd = open("/dev/ecram", O_RDWR);
	if (fd < 0)
	{
		printf("can't open!\n");
	}

	if (argc < 2)
	{
		printf("Parameters error!\n");
		return 0;
	}
	
	if(strcmp(argv[1], "write") == 0)
	{
		if (argc != 4)
		{
			printf("Parameters error!\n");
			return 0;
		}
		else
		{
			val = (strtoul(argv[2],&endstr,16) << 8) | strtoul(argv[3],&endstr,16);
		
			write(fd, &val, 16);
			printf("write addr:0x%x,data:0x%x\n",strtoul(argv[2],&endstr,16),strtoul(argv[3],&endstr,16));
		}
	}
	else if(strcmp(argv[1], "read") == 0)
	{
		memset(key_vals, 0x00, sizeof(key_vals));
		read(fd, key_vals, sizeof(key_vals));
	
		printf("RW 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F\n");
		for(ec_line=0;ec_line<16;ec_line++)
		{
			printf("%x0 ",ec_line);
			for(ec_row=0;ec_row<16;ec_row++)
			{
				index_num = ec_line * 16 + ec_row;
				printf("%02x ",key_vals[index_num]);
			}
			printf("\r\n");
		}
	}
	else
	{
		printf("Useless parameters!!!\n");	
	}

	return 0;
}

